package com.wc.alorithm_luogu._区间合并.校门外的树;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/4/21 10:21
 * @description https://www.luogu.com.cn/problem/P1047
 * 校门外的树
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int tree = sc.nextInt();

        int n = sc.nextInt();

        // 每两个表示树的间距
        LinkedList<Integer> treeList = new LinkedList<>();

        treeList.add(0);
        treeList.add(tree);

        // 要砍树的左边界 右边界
        int cutStart;
        int cutEnd;

        // 当前树的左边界 右边界
        int treeStart;
        int treeEnd;

        // 当前树间距个数
        int size;

        // 存答案
        int ans = 0;
        while (n-- > 0) {

            cutStart = sc.nextInt();
            cutEnd = sc.nextInt();
            size = treeList.size();

            for (int i = 0; i < size; i += 2) {
                treeStart = treeList.get(i);
                treeEnd = treeList.get(i + 1);

                // 表示在砍树的在树间距的最左边
                if (cutEnd < treeStart) {
                    break;
                }
                // 表示start在外面的两种情况
                if (cutStart <= treeStart) {

                    // 左相交
                    if (cutEnd < treeEnd) {
                        treeList.set(i, cutEnd + 1);
                        break;
                    }

                    // 全包围
                    if (cutEnd >= treeEnd) {
                        treeList.remove(i);
                        treeList.remove(i);
                        i -= 2;
                        size-=2;
                    }
                } else {

                    // 被全包围
                    if (cutEnd < treeEnd) {
                        treeList.add(i + 1, cutEnd + 1);
                        treeList.add(i + 1, cutStart - 1);
                        break;
                    }

                    // 右相交
                    if (cutStart <= treeEnd && cutEnd >= treeEnd) {
                        treeList.set(i + 1, cutStart - 1);
                    }
                }
            }
        }
        size = treeList.size();

        // 计算总数
        for (int i = 0; i < size; i += 2) {
            ans += (treeList.get(i + 1) - treeList.get(i) + 1);
        }

        System.out.println(ans);
    }
}
